/*
 * AIData.cpp
 *
 *  Created on: Sep 1, 2017
 *      Author: gaoyi
 */


#include "Character/Creature.h"
#include "Character/newAI/behaviac/CreatureCommonAI.h"
#include "Character/MovePart.h"
#include "AIData.h"
#include "Character/monster/Monster.h"

AIData::AIData(Creature *pMaster)
{
	SetMaster(pMaster);

	SetCurState(AI_STATE_NONE);								//��ǰAI״̬
	SetPreState(AI_STATE_NONE);								//��һ��AI״̬


	uint64_t time = g_GetGlobalServerTime()->Tick();
	SetBornTime(time);										//����ʱ��

	//
	SetBornPos(GetMaster()->GetPos());						//����λ��
	SetBornProtectTime(0);									//��������ʱ��
	SetAttackType(AUTO_ATTACK);								//�������� ����
	SetCanAttack(true);										//�ܷ񹥻�

	SetActiveRange(0);										//���Χ
	SetPursuitRange(0);										//׷����Χ

	SetPatrolType(AI_PATROL_MODE_RANDOM);					//Ѳ������
	SetPatrolStep(-1);										//Ѳ�߲���
	SetCurPatrolPos(Point3<float>());						//Ѳ�ߵ�ǰPos
	SetPatrolBack(false);									//// �Ƿ�����Ѳ��(true����)

	SetRestTimeData(0);										//��Ϣʱ��
	SetPatrolRestTime(0);									//Ѳ����Ϣʱ��

	SetMoveType(AI_MOTION_MODE_WALK);						//�ƶ�����
	SetMoveSpeed(0.0f);										//�ƶ��ٶ�
	SetRunSpeed(0.0f);										//�ܲ��ٶ�

	SetFollowTargetCid(0);									//����״̬��������
}

//��ʼ��
bool AIData::Init()
{
	return false;
}

AIData::~AIData()
{

}

bool AIData::IsMoving()											//�Ƿ������ƶ�
{
	MovePart* part = dynamic_cast<MovePart*>(GetMaster()->GetPart(PART_MOVE));
	if (part == nullptr) return false;

	return part->IsMoving();
}

bool AIData::IsCanRandomMove()										//�ǲ��ǿ���������ƶ�
{
	if (this->GetPatrolType() == AI_PATROL_MODE_RANDOM && this->GetActiveRange() > 0)
	{
		return true;
	}
	return false;
}

bool AIData::IsCanPatrolMove()
{
	if (this->GetPatrolType() >= AI_PATROL_MODE_BACK && this->GetPatrolPath().empty() == false)
	{
		return true;
	}
	return false;
}

Point3<float> AIData::GetPatrolPath(int index)
{
	if (index >= 0 && index < (int)mPatrolPath.size())
	{
		return mPatrolPath[index];
	}
	return Point3<float>();
}

void AIData::StopMove(bool isBroadClient)								//AIֹͣ�ƶ�
{
	GetMaster()->StopMove(isBroadClient);
}

bool AIData::AIMove(const Point3<float>& pos, int walkType)
{
	float CurSpeed = GetMaster()->GetSpeed();
	if (AiLogicUtility::UtilFloatEqualZero(CurSpeed) || GetMoveType() != walkType)
	{
		SetMoveType(walkType);
		if (walkType == AI_MOTION_MODE_WALK)
		{
			GetMaster()->SetSpeed(GetMoveSpeed(), true);
		}
		else
		{
			GetMaster()->SetSpeed(GetRunSpeed(), true);
		}
		return GetMaster()->MoveTo(pos);
	}
	return true;
}

bool AIData::PlayActionOnPatrolPoint(int step)
{
	AiPlayIdleInfo info;
	info.set_cid(GetMaster()->GetCid());
	GetMaster()->BroadCastData(LOGIC_TO_CLIENT_PLAY_IDLE, &info);
	return true;
}
